package com.android.mvp.presenter;

import android.content.Context;

import com.android.mvp.base.BasePresenter;
import com.android.mvp.base.BasePresenterI;
import com.android.mvp.base.BaseViewI;
import com.android.mvp.bean.HomeBanner;
import com.android.mvp.bean.M_Base;
import com.android.mvp.modle.HomeModel;
import com.android.mvp.presenter.contract.HomeContract;
import com.android.mvp.util.MLog;

import java.util.List;

import rx.Subscriber;

/**
 * desc
 *
 * @autor 徐文龙
 * @time 2016/9/8  10:35
 */
public class HomePresenter extends BasePresenter<BaseViewI> implements BasePresenterI {

    String MyUrl,ResultUrl, NameText = "网页链接";

    private HomeModel mModelI;


    public HomePresenter(Context context, HomeContract.View viewI) {
        this.context=context;
        this.mView=viewI;
        mModelI=new HomeModel();
    }

    public void getHomeBanner(){
        mModelI.getHomeBanner().subscribe(new Subscriber<M_Base<List<HomeBanner>>>() {
            @Override
            public void onCompleted() {
                //   hideProgress();
                MLog.v("DaLong","completed");
            }

            @Override
            public void onError(Throwable e) {
                mView.showDataError("网络异常",0);
                MLog.v("DaLong","error" +e.getMessage());
            }

            @Override
            public void onNext(M_Base<List<HomeBanner>> listM_base) {
                MLog.v("DaLong",""+listM_base.getStatus()+"   "+listM_base.getMssage());
                if(listM_base.getStatus()==1){
                    mView.showDataSuccess(listM_base.getData());
                }else{
                    mView.showDataError(listM_base.getMssage(),listM_base.getStatus());
                }

            }
        });

//        String MyUrl="阿斯顿法国大http://www.baidu.com哈哈哈哈http://www.taobao.com啊尔特问问为台湾是附件是打开了http://www.dahge.com";
//
//        this.ResultUrl=MyUrl;
//
//        getResult(MyUrl,MyUrl);


    }



//    public void getResult(String url,String url2){
//        String myUrl=url;
//        String ResultUrl2=url2;
//        Pattern pattern = Pattern
//                .compile("((http|ftp|https)://|www)[a-zA-Z0-9\\._-]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*");
//
//        Matcher matcher = pattern
//                .matcher(myUrl);
//
//        List<String> urlList=new ArrayList<String>();
//        while(matcher.find()){
//            String urls=matcher.group(0);
//            urlList.add(urls);
//        }
//
//
////        Observable.create(new Observable.OnSubscribe<String>() {
////            @Override
////            public void call(Subscriber<? super String> subscriber) {
////                for(int i=0;i<6;i++){
////                    if(i==3){
////                        subscriber.onError(new Throwable("dd"));
////                    }
////                    subscriber.onNext(""+i);
////                }
////                subscriber.onCompleted();
////            }
////        }).subscribe(s -> {
////            MLog.v();
////        });
//
//
//
//
//
////        Observable.from(urlList).flatMap(new Func1<String, Observable<MyUrl>>() {
////            @Override
////            public Observable<MyUrl> call(final String s) {
////                return Observable.create(new Observable.OnSubscribe<MyUrl>() {
////                    @Override
////                    public void call(Subscriber<? super MyUrl> subscriber) {
////                        final String myUrl=s;
////                        String myUrl2;
////                        Pattern pattern = Pattern
////                                .compile("((http|ftp|https)://)(www)[a-zA-Z0-9\\._-]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*");
////                        Matcher matcher = pattern
////                                .matcher(myUrl);
////
////                        if(matcher.find()) {
////                            myUrl2=s;
////                        }else{
////                            myUrl2="http://"+s;
////                        }
////                        MLog.v("DaLong",myUrl2);
////
////
////                        HttpHelper.getInstance().get(s, new HttpHelper.MyResponse() {
////
////                            @Override
////                            public void onResponse(String result) {
////                                String temp=getTitle(result);
////                                if (temp!=null&&temp.length()>0) {
////                                    MLog.v("DaLongSuccess",temp);
////                                    subscriber.onNext(new MyUrl(myUrl,"<title>" + myUrl2 + "<d>" + temp + "</title>"));
////                                    subscriber.onCompleted();
////                                }
////
////                            }
////                            @Override
////                            public void onFailure(IOException e)  {
////                                // TODO Auto-generated method stub
////                                subscriber.onError(new Throwable("哈哈哈哈哈哈"));
////                                //     String temp=myUrl;
////                                //  MLog.v("DaLongError",e.getMessage());
////                                //subscriber.onNext(new MyUrl(myUrl,"<title>" + myUrl2 + "<d>" + temp + "</title>"));
////
//////                                String temp=myUrl;
//////                                MLog.v("DaLongError",temp);
////                                //  subscriber.onNext(new MyUrl(myUrl,"<title>" + myUrl2 + "<d>" + temp + "</title>"));
////                                //                   subscriber.onError(e);
//////                                subscriber.onCompleted();
////                            }
////                        });
////                    }
////                });
////            }
////        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
////
////                .subscribe(new Subscriber<MyUrl>() {
////                    @Override
////                    public void onCompleted() {
////                        MLog.v("DaLongCompleted","哈哈"+ResultUrl);
////
////                    }
////
////                    @Override
////                    public void onError(Throwable e) {
////
////                        MLog.v("DaLongError",e.getMessage());
////                    }
////
////                    @Override
////                    public void onNext(MyUrl bean) {
////                        MLog.v("DaLongNext",bean.getWebUrl()+"            "+bean.getWebTitle());
////                        ResultUrl = ResultUrl.replace(bean.getWebUrl(), bean.getWebTitle());
////                        MyUrl=MyUrl.replace(bean.getWebUrl(),"网络连接");
////                    }
////                });
//    }



//    /**
//     * 从html源码(字符串)中去掉标题
//     *
//     * @param htmlSource
//     * @return
//     */
//    public String getTitle(String htmlSource) {
//        List<String> list = new ArrayList<String>();
//        String title = "";
//
//        Pattern pa = Pattern.compile("<title>.*?</title>");// 源码中标题正则表达式
//        Matcher ma = pa.matcher(htmlSource);
//        while (ma.find())// 寻找符合el的字串
//        {
//            list.add(ma.group());// 将符合el的字串加入到list中
//        }
//        for (int i = 0; i < list.size(); i++) {
//            title = title + list.get(i);
//        }
//        return outTag(title);
//    }
//
//    /**
//     * 去掉html源码中的标签
//     *
//     * @param s
//     * @return
//     */
//    public String outTag(String s) {
//        return s.replaceAll("<.*?>", "");
//    }
}
